package gui;

import io.red.GestorInterfacesRed;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import logica.Nucleo;

/**
 *
 * @author Mariano Zapata
 */
public class DialogoConfiguracion extends javax.swing.JPanel {

    private Nucleo nucleo;
    private boolean retorno;
    
    private static final String ERROR_FORMATO= "Número de puerto incorrecto.\nDebe ser un valor numérico perteneciente " +
            "al intervalo [1025 ; 65536].";
    private static final String ERROR_INTERFACE= "Debe seleccionar una interface de red.";
    
    /** Creates new form DialogoConfiguracion */
    public DialogoConfiguracion(String puertoRMI, List<String> nombresInterfacesRed, String nombreSeleccionar,
            int tipoIP, Nucleo nucleo) {
        initComponents();
        Image icono= new ImageIcon(getClass().getResource("/gui/icono/logo_chico.png")).getImage();
        dialogo.setIconImage(icono);
        dialogo.getContentPane().setLayout(new BorderLayout());
        this.nucleo= nucleo;
        this.puertoRMI.setText(puertoRMI);
        for (String s : nombresInterfacesRed)
            interfacesRed.addItem(s);
        if (!nombreSeleccionar.isEmpty())
            interfacesRed.setSelectedItem(nombreSeleccionar);
        if (tipoIP == GestorInterfacesRed.IPV4)
            tipoIPv4.setSelected(true);
        else
            tipoIPv6.setSelected(true);
        retorno= false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogo = new javax.swing.JDialog();
        grupo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        puertoRMI = new javax.swing.JTextField();
        guardar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        interfacesRed = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tipoIPv4 = new javax.swing.JCheckBox();
        tipoIPv6 = new javax.swing.JCheckBox();

        dialogo.setTitle("Configuración");
        dialogo.setModal(true);
        dialogo.setResizable(false);

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Puerto para servicio RMI:");

        puertoRMI.setToolTipText("Formato: número entero en el intervalo [1025;65536]");

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardar(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Interface de red:");

        interfacesRed.setMaximumRowCount(6);

        jLabel3.setText("Tipo de dirección IP a utilizar:");

        grupo.add(tipoIPv4);
        tipoIPv4.setSelected(true);
        tipoIPv4.setText("IPv4");

        grupo.add(tipoIPv6);
        tipoIPv6.setText("IPv6");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(guardar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(interfacesRed, 0, 527, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(puertoRMI, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tipoIPv6))
                                .addGap(0, 432, Short.MAX_VALUE))
                            .addComponent(tipoIPv4))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(puertoRMI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(interfacesRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tipoIPv4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tipoIPv6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(guardar)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guardar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardar
        try {
            String nombreInterface= (String) interfacesRed.getSelectedItem();
            int tipoIP;
            if (tipoIPv4.isSelected())
                tipoIP= GestorInterfacesRed.IPV4;
            else
                tipoIP= GestorInterfacesRed.IPV6;
            nucleo.configurarParametros(puertoRMI.getText(), nombreInterface, tipoIP);
            retorno= true;
            dialogo.setVisible(false);
        } catch (NumberFormatException ex) {this.error(ERROR_FORMATO);}
          catch (Exception ex) {this.error(ERROR_INTERFACE);}
    }//GEN-LAST:event_guardar

    /**
     * @return si se debe reiniciar el Servidor o no.
     */
    public boolean hacerVisible(Component c)
    {
        dialogo.getContentPane().add(this, BorderLayout.CENTER);
        dialogo.pack();
        dialogo.setLocationRelativeTo(c);
        dialogo.setVisible(true);
        dialogo.dispose();
        return retorno;
    }
    
    private void error(String msj)
    {
        JOptionPane.showMessageDialog(this, msj, "Error", JOptionPane.ERROR_MESSAGE);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog dialogo;
    private javax.swing.ButtonGroup grupo;
    private javax.swing.JButton guardar;
    private javax.swing.JComboBox interfacesRed;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField puertoRMI;
    private javax.swing.JCheckBox tipoIPv4;
    private javax.swing.JCheckBox tipoIPv6;
    // End of variables declaration//GEN-END:variables
}
